package com.darwin.darwinMVC.core
{
	import com.darwin.darwinMVC.mvc.Command;
	import com.darwin.darwinMVC.mvc.DataSource;
	import com.darwin.darwinMVC.utils.CmdLinkedList;
	import com.darwin.darwinMVC.utils.CmdLinkedListItem;
	
	import flash.utils.Dictionary;

	/**
	 * 游戏中间器，游戏模块间的通讯桥梁。所有框架间的通讯通过
	 * @author lipi
	 */
	public class Context
	{
		public function Context()
		{
			ContextPlace.setContext(this);
			_listenerDic = new Dictionary();
			_pauseCmdDic = new Dictionary();
		}
		
		//侦听器列表
		private var _listenerDic:Dictionary;
		
		//被暂停执行的命令列表
		private var _pauseCmdDic:Dictionary;
		
		
		/**
		 * 发布一个命令
		 * @param cmd 命令对象
		 */
		public function dispatch(cmd:Command):void
		{
			if(_listenerDic.hasOwnProperty(cmd.cmdName))
			{
				var $cmdList:CmdLinkedList = (_listenerDic[cmd.cmdName] as CmdLinkedList);
				var $cmdItem:CmdLinkedListItem = $cmdList.firstItem;
				
				executeCmd(cmd,$cmdItem);
			}
		}
		
		/**
		 * 增加一个命令侦听
		 * @param cmdName 命令名
		 * @param listener 侦听对象
		 * @param priority 优先级
		 */
		public function addCmdListener(cmdName:String,listener:IListener,priority:int = 0):void
		{
			if(_listenerDic[cmdName] == null)
			{
				_listenerDic[cmdName] = new CmdLinkedList();
			}
			(_listenerDic[cmdName] as CmdLinkedList).addItem(listener,priority);
		}
		
		/**
		 * 取消一个命令的侦听
		 * @param cmdName 命令名
		 * @param listener 侦听对象
		 */
		public function removeCmdListener(cmdName:String,listener:IListener):void
		{
			(_listenerDic[cmdName] as CmdLinkedList).removeItem(listener);
		}
		
		
		/**
		 * 继续执行一个命令
		 * @param cmd 命令对象
		 */
		public function proceedCmd(cmd:Command):void
		{
			if(_pauseCmdDic[cmd] != null)
			{
				var $cmdItem:CmdLinkedListItem = _pauseCmdDic[cmd] as CmdLinkedListItem;
				delete _pauseCmdDic[cmd];
				executeCmd(cmd,$cmdItem.next);
				
			}
		}
		
		
		/**
		 * 执行命令的侦听器 
		 * @param cmd 命令对象 
		 * @param startItem 开始执行的侦听对象 
		 */
		private function executeCmd(cmd:Command,startItem:CmdLinkedListItem):void
		{
			var $cmdItem:CmdLinkedListItem = startItem;
			while($cmdItem != null)
			{
				if(cmd.isStop)
				{
					break;
				}else if(cmd.isPause){
					break;
				}else{
					$cmdItem.listener.execute(cmd);
					if(cmd.isPause) _pauseCmdDic[cmd] = $cmdItem;//如果当前这个侦听器暂停了cmd，则存入这个对象
					$cmdItem = $cmdItem.next;
				}//if 
			}//while 
		}
		
		
		
		private var _dataSource:DataSource;
		/**
		 * 数据源实例
		 * @return 
		 */
		public function get dataSource():DataSource
		{
			return _dataSource;
		}

		public function set dataSource(value:DataSource):void
		{
			_dataSource = value;
		}
		
		
		private var _commonView:CommonView;
		/**
		 * 公共视图类
		 */
		public function get commonView():CommonView
		{
			return _commonView;
		}

		public function set commonView(value:CommonView):void
		{
			_commonView = value;
		}
		
		
		
		private var _commonData:CommonData;
		/**
		 * 公共数据类
		 */
		public function get commonData():CommonData
		{
			return _commonData;
		}

		public function set commonData(value:CommonData):void
		{
			_commonData = value;
		}
		
		
//		
//		/**
//		 * 克隆一个CmdLinkedListItem列表，即将传入的cmdItem即后续的cmdItem进行克隆
//		 */
//		private function cloneCmdItemList(cmdItem:CmdLinkedListItem):CmdLinkedListItem
//		{
//			var $currentItem:CmdLinkedListItem = cmdItem;
//			var $returnItem:CmdLinkedListItem = new CmdLinkedListItem(cmdItem.listener,cmdItem.priority);//输出命令项
//			var $newItem:CmdLinkedListItem = $returnItem;//新的命令项
//			
//			while($currentItem.next != null)
//			{
//				$returnItem.next = new CmdLinkedListItem($currentItem.next.listener,$currentItem.next.priority);
//				$currentItem = $currentItem.next;
//			}
//			return $returnItem;
//		}
		
	}
}